package service

import (
	systemConfig "gin-worked/app/system/config"
	"gin-worked/app/system/model"
	"github.com/goccy/go-json"
	"strconv"
	"time"
)

type MenuListApi struct {
	List interface{}
}

// ListMenuTree 获取菜单树结构
func ListMenuTree(uid int) Res {
	systemRole := &model.SystemRole{}

	systemRole.SystemId = uid
	systemRole.Status = strconv.Itoa(1)
	systemRole.DeleteTime = strconv.Itoa(0)
	systemRoleField := []string{"id", "system_id", "role_id"} //查询字段
	systemRoleInfo, _ := model.SystemRoleInfo(systemRole, systemRoleField)

	if systemRoleInfo.Id == 0 {
		return Res{0, "你还没有角色,请联系管理员", ""}
	}

	var res []model.Menu

	if systemRoleInfo.Role.IsAdmin == 1 {
		where := []interface{}{
			[]interface{}{"status", "=", 1},
			[]interface{}{"delete_time", "=", 0},
			[]interface{}{"is_menu", "=", 0},
		}

		field := []string{"id", "name", "status", "is_menu", "url", "web_url", "sort", "icon", "parent_id"} //查询字段
		order := "sort ASC"
		res, _ = model.MenuListAll(where, field, order)

	} else {
		menuRoleWhere := &model.MenuRole{}
		menuRoleWhere.RoleId = systemRoleInfo.Role.Id
		field1 := []string{"role_id", "menu_id"} //查询字段
		list, _ := model.MenuRoleList(menuRoleWhere, field1)

		for _, value := range list {
			if value.Menu.Id == 0 {
				continue
			}

			if value.Menu.IsMenu == "0" {
				res = append(res, value.Menu)
			}
		}
		return Res{1, "success", res}
	}
	var results []map[string]interface{}
	for _, menu := range res {
		bytes, _ := json.Marshal(menu)
		var m map[string]interface{}
		_ = json.Unmarshal(bytes, &m)
		results = append(results, m)
	}
	results1 := systemConfig.GetChildren(results)

	return Res{1, "success", results1}

}

// ListMenu 获取菜单
func ListMenu() Res {
	where := []interface{}{
		[]interface{}{"delete_time", "=", 0},
	}

	field := []string{"id", "name", "status", "is_menu", "url", "web_url", "sort", "icon", "parent_id"} //查询字段
	order := "sort ASC"

	list, _ := model.MenuListAll(where, field, order)
	var results []map[string]interface{}
	for _, menu := range list {
		bytes, _ := json.Marshal(menu)
		var m map[string]interface{}
		_ = json.Unmarshal(bytes, &m)
		results = append(results, m)
	}

	results1 := systemConfig.GetChildren(results)

	return Res{1, "success", results1}
}

// MenuAdd 添加菜单
func MenuAdd(name string, isMenu string, url string, sort string, status string, parentId string, icon string, webUrl string) Res {

	sortEs, _ := strconv.Atoi(sort)
	statusEs, _ := strconv.Atoi(status)
	ParentId, _ := strconv.Atoi(parentId)

	times := time.Now().Unix()

	data := &model.Menu{
		Name:       name,
		Url:        url,
		Sort:       sortEs,
		Status:     statusEs,
		ParentId:   ParentId,
		Icon:       icon,
		IsMenu:     isMenu,
		WebUrl:     webUrl,
		DeleteTime: 0,
		CreateTime: strconv.FormatInt(times, 10),
		UpdateTime: strconv.FormatInt(times, 10),
	}

	res, err := model.MenuAdd(data)

	if err != nil {
		return Res{0, err.Error(), ""}
	}

	return Res{1, "success", res}
}

// MenuInfo 获取菜单详情
func MenuInfo(id string) Res {

	ids, _ := strconv.Atoi(id)
	data := &model.Menu{
		Id: ids,
	}
	field := []string{"id", "name", "status", "is_menu", "url", "web_url", "sort", "icon", "parent_id"} //查询字段
	info, err := model.MenuInfo(data, field)

	if err != nil {
		return Res{0, err.Error(), ""}
	}
	return Res{1, "success", info}
}

// MenuEdit 编辑菜单
func MenuEdit(id string, name string, isMenu string, url string, sort string, status string, parentId string, icon string, webUrl string) Res {

	sortEs, _ := strconv.Atoi(sort)
	statusEs, _ := strconv.Atoi(status)
	ParentId, _ := strconv.Atoi(parentId)
	ids, _ := strconv.Atoi(id)

	times := time.Now().Unix()
	data := &model.Menu{
		Id: ids,
	}

	var content = map[string]interface{}{"name": name, "is_menu": isMenu, "url": url, "sort": sortEs, "status": statusEs, "parent_id": ParentId, "icon": icon, "web_url": webUrl, "update_time": times}
	err := model.MenuEdit(data, content)
	if err != nil {
		return Res{0, err.Error(), ""}
	}

	return Res{1, "success", ""}

}

func MenuSetStatus(id string, status string) Res {
	statusEs, _ := strconv.Atoi(status)
	ids, _ := strconv.Atoi(id)
	data := &model.Menu{
		Id: ids,
	}
	var content = map[string]interface{}{"status": statusEs}

	err := model.MenuEdit(data, content)

	if err != nil {
		return Res{0, err.Error(), ""}
	}
	return Res{1, "success", ""}
}

func MenuDel(id string) Res {
	ids, _ := strconv.Atoi(id)
	data := &model.Menu{
		Id: ids,
	}
	times := time.Now().Unix()
	var content = map[string]interface{}{"delete_time": times}

	err := model.MenuEdit(data, content)

	if err != nil {
		return Res{0, err.Error(), ""}
	}
	return Res{1, "success", ""}
}
